package pl.com.qapps.datastore.base;

import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.ExecutorService;

import pl.com.qapps.datastore.Datastore;
import pl.com.qapps.datastore.Store;

import com.google.appengine.api.datastore.AsyncDatastoreService;
import com.google.appengine.api.datastore.DatastoreFailureException;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceConfig;

// TODO implement base functionality
/**
 * 
 * @author kuba
 * 
 */
public abstract class DatastoreBase implements Datastore {

	private final Store store;
	private final ExecutorService dispatcher;

	public DatastoreBase(Store store, ExecutorService dispatcher) {
		this.store = Objects.requireNonNull(store, "store cannot be null");
		this.dispatcher = Objects.requireNonNull(dispatcher,
				"dispatcher cannot be null");
	}

	@Override
	public DatastoreService getDatastoreService(DatastoreServiceConfig cfg) {
		// TODO Auto-generated method stub
		return new DatastoreServiceImpl(cfg, store, null, dispatcher);
	}

	@Override
	public AsyncDatastoreService getAsyncDatastoreService(
			DatastoreServiceConfig cfg) {
		// TODO Auto-generated method stub
		return new AsyncDatastoreServiceBase(cfg, store, null, dispatcher);
	}

	@Override
	public void close() throws DatastoreFailureException {
		try {
			store.close();
		} catch (IOException e) {
			// TODO is this exception handling right?
			throw new DatastoreFailureException(e.getMessage());
		}
	}

}
